# 多阶段构建，先使用一个 go 容器来编译程序
FROM golang:1.23.3-alpine3.20 as builder
WORKDIR /
COPY . .
RUN go env -w GO111MODULE=on
RUN go env -w GOPROXY=https://goproxy.cn,direct
RUN go build -o bookinfo

# 运行应用的容器不需要 go 环境
FROM alpine:3.20
WORKDIR /bookinfo

# 换源，加快下载
RUN sed -i 's#https\?://dl-cdn.alpinelinux.org/alpine#https://mirrors.tuna.tsinghua.edu.cn/alpine#g' /etc/apk/repositories
# --no-cache 表示不保留缓存，为了减小镜像体积
RUN apk --update --no-cache add curl

COPY template ./template
COPY static ./static
COPY --from=builder /bookinfo ./
EXPOSE 9061
RUN chmod +x ./bookinfo
ENTRYPOINT [ "/bookinfo/bookinfo" ]
CMD [ "serve" ]
